/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.delivery.dao;

import com.mycompany.delivery.entities.Account;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

/**
 *
 * @author mhunek
 */
@Repository(value = "accountDao")
public class AccountDAOImpl implements AccountDAO {

    @PersistenceContext(unitName = "TestPU")
    private EntityManager manager;

    public EntityManager getManager() {
        return manager;
    }

    public void setManager(EntityManager manager) {
        this.manager = manager;
    }

    public Account getByLogin(String login) {
        Account result = null;
        final String qstring = "SELECT e FROM Account e WHERE e.login = :login";
        TypedQuery<Account> query = manager.createQuery(qstring, Account.class);
        query.setParameter("login", login);
        List<Account> accounts = query.getResultList();
        if (accounts.size() != 1) {
                return null;
            } else {
                return accounts.get(0);
            }
    }

    @Override
    public Account create(Account account) {
        try {
            if (account != null) {
                manager.persist(account);
            }
        } catch (NullPointerException | IllegalArgumentException e) {
            throw new DataAccessException("Problem with argumentL" + e.getMessage()) {
            };
        } catch (Exception ex) {
            throw new DataAccessException(ex.getMessage()) {
            };
        }
        return account;
    }
    
    @Override
    public void updateAccount(Account account) 
    {
        try
        {
            manager.merge(account);
        }
        catch (NullPointerException | IllegalArgumentException e) 
        {
            throw new DataAccessException("Problem with argumentL" + e.getMessage()) {
            };
        }
        catch (Exception ex) 
        {
            throw new DataAccessException(ex.getMessage()) {
            };
        }
    }
    
    
     @Override
    public void deleteAccount(Account account) 
    {
        try
        {
            manager.remove(manager.find(Account.class, account.getId()));
        }
        catch (NullPointerException | IllegalArgumentException e) 
        {
            throw new DataAccessException("Problem with argumentL" + e.getMessage()) {
            };
        }
        catch (Exception ex) 
        {
            throw new DataAccessException(ex.getMessage()) {
            };
        }
    }

    
     @Override
    public Account getAccountById(Long id)
    {
        Account account;
        try
        {
            account = (Account) manager.find(Account.class, id);
        }
        catch (NullPointerException | IllegalArgumentException e) 
        {
            throw new DataAccessException("Problem with argumentL" + e.getMessage()) {
            };
        }
        catch (Exception ex) 
        {
            throw new DataAccessException(ex.getMessage()) {
            };
        }
        return (Account) account;
    }
    
    
     @Override
    public List<Account> getAllContacts() 
    {
        List<Account> account;
        try
        {
            account =  manager.createQuery("SELECT c FROM Account c").getResultList();
        }
              catch (NullPointerException | IllegalArgumentException e) 
        {
            throw new DataAccessException("Problem with argumentL" + e.getMessage()) {
            };
        } 
        catch (Exception ex) 
        {
            throw new DataAccessException(ex.getMessage()) {
            };
        }
        return  account;
    }

}
